package com.selfdriving.dialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.TextView;

import com.selfdriving.BaseApplication;
import com.selfdriving.BaseDialog;
import com.selfdriving.R;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.WriteScenic;
import com.selfdriving.wl.activity.SimpleListDialog;
import com.selfdriving.wl.activity.SimpleListDialog.onSimpleListItemClickListener;

public class ChooseDialog extends BaseDialog implements
		android.view.View.OnClickListener, OnCheckedChangeListener
{

	private OnDialogClickListener clickListener;
	private setOnDialogTwoClickListener onDialogTwoClickListener;
	private OnBirthdayDialogClickListener onBirthdayDialogClickListener;
	private OnTogetherDialogClickListener onTogetherDialogClickListener;
	private Button btnSure;
	private Button btnCancel;
	private EditText username;
	private DatePicker myBirthday;
	private Calendar ca;
	private String birthday;
	private String user = "";
	RadioButton button1;
	RadioButton button2;
	RadioButton button3;
	LinearLayout setTime;
	LinearLayout setAge;
	LinearLayout setSex;
	LinearLayout setHaveCar;
	LinearLayout setTgogetherMode;
	SimpleListDialog setTimeDialog;
	Context context;
	String sexs[] = { "不限", "男", "女" };
	String times[] = { "不限", "三日内", "一周内", "一月内" };
	List<Entity> togetherWay = new ArrayList<Entity>();
	List<Entity> carList = new ArrayList<Entity>();
	List<Entity> ageList = new ArrayList<Entity>();
	String sex = "";
	String dayId = "";
	String togetherWayId = "";
	String carId = "";
	BaseApplication application;
	ChooseDialog dialog;
	private RatingBar ratingBar;
	private int starNum = 1;
	String st;

	public ChooseDialog(BaseApplication application, Context context, String st)
	{
		super(context);
		this.application = application;
		this.context = context;
		getTogetherWayList();
		this.st = st;
		setDialog(st);
	}

	public void getTogetherWayList()
	{
		togetherWay.clear();
		togetherWay.add(new WriteScenic("不限", "9"));
		togetherWay.add(new WriteScenic("单对单", "0"));
		togetherWay.add(new WriteScenic("情人结伴", "1"));
		togetherWay.add(new WriteScenic("家庭结伴", "2"));
		togetherWay.add(new WriteScenic("多对多", "3"));
		carList.clear();
		carList.add(new WriteScenic("无车", "0"));
		carList.add(new WriteScenic("有车", "1"));
		ageList.add(new WriteScenic("不限", "9"));
		ageList.add(new WriteScenic("18-25岁", "0"));
		ageList.add(new WriteScenic("26-30", "1"));
		ageList.add(new WriteScenic("31-35", "2"));
		ageList.add(new WriteScenic("36-40", "3"));
		ageList.add(new WriteScenic("41-45", "4"));
		ageList.add(new WriteScenic("46-50", "5"));
	}

	public ChooseDialog(Context context, String st, String user)
	{
		super(context);
		this.user = user;
		this.context = context;
		this.st = st;
		setTestMsg(st);
	}

	public void setTestMsg(String st)
	{
		setDialogContentView(R.layout.choose_name_dialog);
		setTitle(st);
		setTitleLineVisibility(View.GONE);
		btnSure = (Button) findViewById(R.id.btnSure);
		btnSure.setOnClickListener(this);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(this);
		username = (EditText) findViewById(R.id.et_username);
		if (st.equals("设置名字"))
		{
			username.setMaxLines(10);
		}
		if (st.equals("招募人数"))
		{
			username.setInputType(InputType.TYPE_CLASS_NUMBER);
		}
		if (st.equals("结伴天数"))
		{
			username.setInputType(InputType.TYPE_CLASS_NUMBER);
		}
		if (st.equals("发布评论"))
		{
			ratingBar = (RatingBar) findViewById(R.id.discuss_ratingbar);
			username.setMinLines(3);
			username.setMaxLines(3);
			username.setGravity(Gravity.TOP);
			ratingBar.setVisibility(View.VISIBLE);
			ratingBar
					.setOnRatingBarChangeListener(new OnRatingBarChangeListenerImpl());
		}

		username.setText(user);
	}

	private class OnRatingBarChangeListenerImpl implements
			RatingBar.OnRatingBarChangeListener
	{
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser)
		{
			starNum = (int) rating; // 取得当前值
		}
	}

	public void setDialog(String st)
	{
		if (st.equals("结伴游"))
		{
			setDialogContentView(R.layout.choose_friend);
			setTitle("筛选");
			setTitleLineVisibility(View.GONE);
			btnSure = (Button) findViewById(R.id.btnSure1);
			btnSure.setOnClickListener(this);
			btnCancel = (Button) findViewById(R.id.btnCancel1);
			btnCancel.setOnClickListener(this);
			setHaveCar = (LinearLayout) findViewById(R.id.setHaveCar);
			setTime = (LinearLayout) findViewById(R.id.setTime);
			setSex = (LinearLayout) findViewById(R.id.setSex);
			setAge = (LinearLayout) findViewById(R.id.setAge);
			setTgogetherMode = (LinearLayout) findViewById(R.id.setGogetherMode);
			setHaveCar = (LinearLayout) findViewById(R.id.setHaveCar);
			setSex.setOnClickListener(this);
			setTgogetherMode.setOnClickListener(this);
			setTime.setOnClickListener(this);
			setAge.setOnClickListener(this);
			setHaveCar.setOnClickListener(this);
		} else if (st.equals("设置日期"))
		{
			setDialogContentView(R.layout.choose_birthday_dialog);
			setTitleLineVisibility(View.GONE);
			btnSure = (Button) findViewById(R.id.btnSure_birthday);
			btnSure.setOnClickListener(this);
			myBirthday = (DatePicker) findViewById(R.id.my_date);
			ca = Calendar.getInstance();
			birthday = ca.get(Calendar.YEAR) + "-"
					+ (ca.get(Calendar.MONTH) + 1) + "-"
					+ ca.get(Calendar.DAY_OF_MONTH);
			myBirthday.init(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH),
					ca.get(Calendar.DAY_OF_MONTH),
					new DatePicker.OnDateChangedListener()
					{
						// 监听选择的出生日期
						@Override
						public void onDateChanged(DatePicker view, int year,
								int monthOfYear, int dayOfMonth)
						{
							// TODO Auto-generated method stub
							ca.set(year, monthOfYear, dayOfMonth);// 改变日期
							birthday = year + "-" + (monthOfYear + 1) + "-"
									+ dayOfMonth;
							// 取得值
						}
					});
		} else if (st.equals("设置性别"))
		{
			setDialogContentView(R.layout.choose_sex_dialog);
			setTitle("修改性别");
			setTitleLineVisibility(View.GONE);
			btnSure = (Button) findViewById(R.id.btnNan);
			btnSure.setOnClickListener(this);
			btnCancel = (Button) findViewById(R.id.btnNv);
			btnCancel.setOnClickListener(this);
		} else if (st.equals("选择文章类型"))
		{
			setDialogContentView(R.layout.choose_sex_dialog);
			setTitle("选择文章类型");
			setTitleLineVisibility(View.GONE);
			btnSure = (Button) findViewById(R.id.btnNan);
			btnSure.setOnClickListener(this);
			btnCancel = (Button) findViewById(R.id.btnNv);
			btnCancel.setOnClickListener(this);
			btnSure.setText("游记");
			btnCancel.setText("攻略");
		}
	}

	@Override
	public void onClick(View v)
	{
		super.onClick(v);
		switch (v.getId())
		{
		case R.id.setGogetherMode:
			setTimeDialog = new SimpleListDialog(context);
			setTimeDialog.setTitle("选择结伴方式");
			setTimeDialog.setTitleLineVisibility(View.GONE);
			setTimeDialog.setAdapter(new EntityListDialogAdapter(application,
					context, togetherWay));
			setTimeDialog
					.setOnSimpleListItemClickListener(new OnSimpleListItemClickListener(
							4));
			setTimeDialog.show();
			break;
		case R.id.setSex:
			setTimeDialog = new SimpleListDialog(context);
			setTimeDialog.setTitle("选择结伴用户性别");
			setTimeDialog.setTitleLineVisibility(View.GONE);
			setTimeDialog
					.setAdapter(new SimpleListDialogAdapter(context, sexs));
			setTimeDialog
					.setOnSimpleListItemClickListener(new OnSimpleListItemClickListener(
							3));
			setTimeDialog.show();
			break;
		case R.id.setTime:
			setText(v);
			break;
		case R.id.setAge:
			// setTimeDialog = new SimpleListDialog(context);
			// setTimeDialog.setTitle("选择性别");
			// setTimeDialog.setTitleLineVisibility(View.GONE);
			// setTimeDialog
			// .setAdapter(new SimpleListDialogAdapter(context, ages));
			// setTimeDialog
			// .setOnSimpleListItemClickListener(new
			// OnSimpleListItemClickListener(
			// 1));
			// setTimeDialog.show();
			break;
		case R.id.setHaveCar:
			setTimeDialog = new SimpleListDialog(context);
			setTimeDialog.setTitle("选择是否有车");
			setTimeDialog.setTitleLineVisibility(View.GONE);
			setTimeDialog.setAdapter(new EntityListDialogAdapter(application,
					context, carList));
			setTimeDialog
					.setOnSimpleListItemClickListener(new OnSimpleListItemClickListener(
							2));
			setTimeDialog.show();
			break;
		case R.id.btnSure:
			if (st.equals("发布评论"))
			{
				onDialogTwoClickListener.OnOkClick(st, "" + starNum);
			} else
			{
				String st = username.getText().toString();
				clickListener.OnOkClick(st);
			}
			break;
		case R.id.btnCancel:
			if (st.equals("发布评论"))
			{
				onDialogTwoClickListener.OnCancelClick();
			} else
			{
				clickListener.OnCancelClick();
			}
			break;
		case R.id.btnSure1:
			onTogetherDialogClickListener.OnOkClick(sex, dayId, "", carId,
					togetherWayId);
			break;
		case R.id.btnCancel1:
			onTogetherDialogClickListener.OnCancelClick();
			break;
		case R.id.btnSure_birthday:
			onBirthdayDialogClickListener.OnOkClick(birthday);
			break;
		case R.id.btnNan:
			onBirthdayDialogClickListener.OnOkClick(btnSure.getText()
					.toString());
			break;
		case R.id.btnNv:
			onBirthdayDialogClickListener.OnOkClick(btnCancel.getText()
					.toString());
			break;
		}
	}

	public void setText(View v)
	{
		LinearLayout layout = (LinearLayout) v;
		final TextView text = (TextView) layout.getChildAt(1);
		TextView text0 = (TextView) layout.getChildAt(0);
		final String title = text0.getText().toString();
		dialog = new ChooseDialog(context, title, text.getText().toString());
		dialog.setOnDialogClickListener(new OnDialogClickListener()
		{
			@Override
			public void OnOkClick(String st)
			{
				if (st != null && !st.equals(""))
				{
					text.setText(st);
					dayId = st;
				}
				dialog.dismiss();
			}

			@Override
			public void OnCancelClick()
			{
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	public void setOnDialogTwoClickListener(
			setOnDialogTwoClickListener onDialogTwoClickListener)
	{
		this.onDialogTwoClickListener = onDialogTwoClickListener;
	}

	public interface setOnDialogTwoClickListener
	{
		void OnOkClick(String st, String st1);

		void OnCancelClick();
	}

	public void setOnDialogClickListener(OnDialogClickListener clickListener)
	{
		this.clickListener = clickListener;
	}

	public interface OnDialogClickListener
	{
		void OnOkClick(String st);

		void OnCancelClick();
	}

	public void setOnBirthdayDialogClickListener(
			OnBirthdayDialogClickListener onBirthdayDialogClickListener)
	{
		this.onBirthdayDialogClickListener = onBirthdayDialogClickListener;
	}

	public interface OnBirthdayDialogClickListener
	{
		void OnOkClick(String st);
	}

	/** 结伴游筛选 */
	public void setOnTogetherDialogClickListener(
			OnTogetherDialogClickListener onTogetherDialogClickListener)
	{
		this.onTogetherDialogClickListener = onTogetherDialogClickListener;
	}

	public interface OnTogetherDialogClickListener
	{
		void OnOkClick(String sex, String time, String age, String car,
				String togetherWayId);

		void OnCancelClick();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	{
		if (isChecked)
		{
			sex = buttonView.getText().toString();
		}
	}

	private class OnSimpleListItemClickListener implements
			onSimpleListItemClickListener
	{
		int x = 0;

		public OnSimpleListItemClickListener(int x)
		{
			this.x = x;
		}

		@Override
		public void onItemClick(int position)
		{
			TextView text;
			WriteScenic scenic;
			switch (x)
			{
			case 1:
				// text = (TextView) setAge.getChildAt(1);
				// text.setText(ages[position]);
				break;
			case 2:
				text = (TextView) setHaveCar.getChildAt(1);
				scenic = (WriteScenic) carList.get(position);
				text.setText(scenic.getName());
				carId = scenic.getId();
				break;
			case 3:
				text = (TextView) setSex.getChildAt(1);
				text.setText(sexs[position]);
				sex = sexs[position];
				break;
			case 4:
				text = (TextView) setTgogetherMode.getChildAt(1);
				scenic = (WriteScenic) togetherWay.get(position);
				text.setText(scenic.getName());
				togetherWayId = scenic.getId();
				break;
			}
			setTimeDialog.dismiss();
		}
	}
}
